package com.module.codeBind;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import base.util.ConstantUnit;
import base.util.Page;
import base.util.StringUtil;
import base.util.Tools;

import com.module.businessLog.BusinessLogService;
import com.module.businessLog.vo.LogContent;
import com.module.codeBind.manager.CodeBindManager;
import com.module.codeBind.vo.CodeBind;
import com.module.codeManage.TdcodeOrderService;
import com.module.sys.LoginSession;

public class CodeBindService {

	private TdcodeOrderService tdcodeOrderService;

	private BusinessLogService logService;

	static Logger logger = Logger.getLogger(CodeBindService.class);

	public CodeBindService() {
	}

	/**
	 * 分页查询码绑定信息
	 * 
	 * @param codeApp
	 *            码绑定对象
	 * @param start
	 *            开始分页行
	 * @param limit
	 *            每一页的最大行数
	 * @return 对象结果集
	 * @throws Exception
	 */
	public Page toList(CodeBind codeBind, final Integer start,
			final Integer limit) throws Exception {
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		codeBind.setOrgId(LoginSession.getOrgids());
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put(ConstantUnit.O_PT, "toListCodeBind");
//		map.put(ConstantUnit.PAGE, page);
//		map.put("codebind", codeBind);
//		return CodeBindSocketClient.returnValue(map).getPage();
		CodeBindManager codeBindManager = CodeBindManager.getInstance();
		page = codeBindManager.toList(codeBind, page);
		return page;
		
	}
//
	/**
	 * 根据ID查询对象
	 * @param id 码绑定对象ID
	 * @return 码绑定对象
	 * @throws Exception
	 */
	public CodeBind findById(String id) throws Exception {
//		Map<String, Object> map = new HashMap<String, Object>();
		CodeBind codeBind = new CodeBind();
		codeBind.setId(id);
//		map.put(ConstantUnit.O_PT, "findBindById");
//		map.put("codebind", codeBind);
//		return CodeBindSocketClient.returnValue(map).getCodebind();
		CodeBindManager codeBindManager = CodeBindManager.getInstance();
		codeBind = codeBindManager.get(id);
		return codeBind;
	}
//
	/**
	 * 新增修改码绑定操作
	 * 
	 * @param code
	 *            绑定用对象
	 * @param bean
	 *            码管理公共对象
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> save(CodeBind code) throws Exception {
		if (Tools.isBlank(code.getId())) {
			code.setOrgId(LoginSession.getOrgid());
			code.setCreaterId(LoginSession.getUserid());
		} else {
			code.setUpdaterId(LoginSession.getUserid());
		}
		Map<String, Object> map = new HashMap<String, Object>();
		CodeBindManager codeBindManager = CodeBindManager.getInstance();
		Map<String, Object> rusultMap = codeBindManager.saveCodeBind(code);
		return rusultMap;
	}
//
	/**
	 * 根据ID批量删除
	 * 
	 * @param idArray
	 *            ID数组
	 * @return Map对象
	 * @throws Exception
	 */
	public Map<String, Object> remove(List<String> idArray) throws Exception {
		String ids = StringUtil.getNewStrByList(idArray);
		CodeBind obj = new CodeBind();
		obj.setId(ids);
		obj.setOrgId(LoginSession.getOrgid());
		Map<String, Object> map = new HashMap<String, Object>();
		CodeBindManager codeBindManager = CodeBindManager.getInstance();
		map = codeBindManager.delCodeBind(obj);
		return map;
	}
//
//	/**
//	 * 返回错误结果
//	 * 
//	 * @param isSave
//	 *            是否是保持
//	 * @return Map对象
//	 */
//	public Map<String, Object> errorResult(String status) {
//		Map<String, Object> statusMap = new HashMap<String, Object>();
//		statusMap.put("state", false);
//		statusMap.put("msg", status);
//		return statusMap;
//	}
//
//	/**
//	 * 记录日志
//	 * 
//	 * @param map
//	 * @param man
//	 * @param id
//	 * @throws Exception
//	 */
//	private void saveLog(Map<String, Object> map, String id, String logcontent)
//			throws Exception {
//		boolean isTrue = (Boolean) map.get("state");
//		LogContent content = new LogContent();
//		if (isTrue) {
//			content.setBusinessCode("codeBind");
//			content.setBusinessContent(logcontent);
//			logService.save(content, id);
//		}
//	}
//
//	/**
//	 * 根据绑定获取对应的码应用
//	 * 
//	 * @param code
//	 *            绑定对象
//	 * @return 返回码应用结果集
//	 * @throws Exception
//	 */
//	public List<Object> getAppCodeByBind(CodeBind code) throws Exception {
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put(ConstantUnit.O_PT, "findBindCodeApp");
//		map.put("codebind", code);
//		return CodeBindSocketClient.returnValue(map).getList();
//	}

	public TdcodeOrderService getTdcodeOrderService() {
		return tdcodeOrderService;
	}

	public void setTdcodeOrderService(TdcodeOrderService tdcodeOrderService) {
		this.tdcodeOrderService = tdcodeOrderService;
	}

	public BusinessLogService getLogService() {
		return logService;
	}

	public void setLogService(BusinessLogService logService) {
		this.logService = logService;
	}
}
